package com.loongxun.playwithyou.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.App;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Member;
import com.loongxun.playwithyou.ui.activity.MemberInfoActivity;
import com.loongxun.playwithyou.util.DensityUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by Administrator on 2017/7/13.
 */
public class MemberAdapter extends BaseAdapter{

    private Context context;
    public List<Member> list;
    private LayoutInflater inflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();

    public MemberAdapter(Context context, List<Member> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public int getCount() {
        if(list.size()%2>0) {
            return list.size()/2+1;
        } else {
            return list.size()/2;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_member_index, null);

            holder.ll_member_left = (LinearLayout) convertView.findViewById(R.id.ll_member_left);
            holder.ll_member_right = (LinearLayout) convertView.findViewById(R.id.ll_member_right);

            holder.iv_member_img_left = (ImageView) convertView.findViewById(R.id.iv_member_img_left);
            holder.iv_member_img_right = (ImageView) convertView.findViewById(R.id.iv_member_img_right);
            ViewGroup.LayoutParams params = holder.iv_member_img_left.getLayoutParams();
            params.width = (getDisplayMetricsWidth() - DensityUtils.dp2px(context, 46))/2;
            params.height = (int) (params.width*4/3);
            holder.iv_member_img_left.setLayoutParams(params);
            holder.iv_member_img_right.setLayoutParams(params);

            holder.tv_member_name_left = (TextView) convertView.findViewById(R.id.tv_member_name_left);
            holder.tv_member_name_right = (TextView) convertView.findViewById(R.id.tv_member_name_right);
            holder.tv_member_dizhi_left = (TextView) convertView.findViewById(R.id.tv_member_dizhi_left);
            holder.tv_member_dizhi_right = (TextView) convertView.findViewById(R.id.tv_member_dizhi_right);
            holder.tv_member_duihao_left = (TextView) convertView.findViewById(R.id.tv_member_duihao_left);
            holder.tv_member_duihao_right = (TextView) convertView.findViewById(R.id.tv_member_duihao_right);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if(position*2+1 < list.size()) {
            final Member item1 = list.get(position*2);
            final Member item2 = list.get(position*2+1);

            holder.ll_member_right.setVisibility(View.VISIBLE);
            if (item1.getMember_name().equals("null")){
                item1.setMember_name("");
            }
            if (item2.getMember_name().equals("null")){
                item2.setMember_name("");
            }
            if (item1.getMember_address().equals("null")){
                item1.setMember_address("");
            }
            if (item2.getMember_address().equals("null")){
                item2.setMember_address("");
            }
            holder.tv_member_name_left.setText(item1.getMember_name());
            holder.tv_member_name_right.setText(item2.getMember_name());
//            holder.tv_member_dizhi_left.setText(item1.getMember_address());
//            holder.tv_member_dizhi_right.setText(item2.getMember_address());
            holder.tv_member_dizhi_left.setText("平台签约教练");
            holder.tv_member_dizhi_right.setText("平台签约教练");
            holder.tv_member_duihao_left.setText(item1.getClick_num()+"");
            holder.tv_member_duihao_right.setText(item2.getClick_num()+"");

            imageLoader.displayImage(ApiInterface.IMAGES + item1.getMember_img(), holder.iv_member_img_left, App.options);
            imageLoader.displayImage(ApiInterface.IMAGES + item2.getMember_img(), holder.iv_member_img_right, App.options);

            holder.ll_member_left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, MemberInfoActivity.class);
                    intent.putExtra("member_id", item1.getMember_id());
                    context.startActivity(intent);
                }
            });

            holder.ll_member_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, MemberInfoActivity.class);
                    intent.putExtra("member_id", item2.getMember_id());
                    context.startActivity(intent);
                }
            });
        }else {
            final Member item1 = list.get(position*2);
            holder.ll_member_right.setVisibility(View.INVISIBLE);
            if (item1.getMember_name().equals("null")){
                item1.setMember_name("");
            }
            if (item1.getMember_address().equals("null")){
                item1.setMember_address("");
            }
            holder.tv_member_name_left.setText(item1.getMember_name());
//            holder.tv_member_dizhi_left.setText(item1.getMember_address());
            holder.tv_member_dizhi_left.setText("平台签约教练");
            holder.tv_member_duihao_left.setText(item1.getClick_num()+"");

            imageLoader.displayImage(ApiInterface.IMAGES + item1.getMember_img(), holder.iv_member_img_left, App.options);

            holder.ll_member_left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, MemberInfoActivity.class);
                    intent.putExtra("member_id", item1.getMember_id());
                    context.startActivity(intent);
                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        private LinearLayout ll_member_left,ll_member_right;
        private ImageView iv_member_img_left,iv_member_img_right;
        private TextView tv_member_name_left,tv_member_name_right,
                tv_member_dizhi_left,tv_member_dizhi_right,
                tv_member_duihao_left,tv_member_duihao_right;
    }

    //获取屏幕宽度
    public int getDisplayMetricsWidth() {
        int i = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
        int j = ((Activity)context).getWindowManager().getDefaultDisplay().getHeight();
        return Math.min(i, j);
    }

}
